package com.nikkerball.apps.darkhorse.web.classroom;

import com.nikkerball.apps.darkhorse.dao.BaseDao;
import com.nikkerball.apps.darkhorse.dao.ModelDao;
import com.nikkerball.apps.darkhorse.model.base.TouchableObject;
import com.nikkerball.apps.darkhorse.model.core.Classroom;
import com.nikkerball.apps.darkhorse.web.base.BasePanel;
import com.nikkerball.apps.darkhorse.web.components.DarkHorseAjaxEditableLabel;
import com.nikkerball.apps.darkhorse.web.components.DeleteConfirmModalPanel;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.Iterator;
import java.util.List;

/**
 * An initial ClassRoom List Panel - this should be replaced by a Generic panel with a listener
 *
 * @author marius (created on 2010-11-28)
 */

public class ClassroomListPanel extends BasePanel {

    // ----- instance fields -----

    @SuppressWarnings({"UnusedDeclaration"})
    @SpringBean
    private BaseDao baseDao;

    @SuppressWarnings({"UnusedDeclaration"})
    @SpringBean
    private ModelDao modelDao;

    private boolean editMode;

    // ----- constructors -----

    public ClassroomListPanel(String id) {
        super(id);
        setOutputMarkupId(true);

        add(new DataView<Classroom>("classrooms", new ClassRoomDataProvider(this.modelDao)) {
            @Override
            protected void populateItem(Item<Classroom> item) {
                Classroom classroom = item.getModelObject();
                if (ClassroomListPanel.this.editMode) {
                    item.add(new DarkHorseAjaxEditableLabel("name", classroom, ClassroomListPanel.this.baseDao));
                } else {
                    item.add(new Label("name", classroom.getName()));
                }

                final ModalWindow deleteConfirmModalPanel;
                item.add(deleteConfirmModalPanel = new ModalWindow("deleteConfirmModalPanel"));

                deleteConfirmModalPanel.setContent(new DeleteConfirmModalPanel(deleteConfirmModalPanel.getContentId(),
                        new Model<TouchableObject>(classroom)));
                deleteConfirmModalPanel.setTitle("This is modal window with panel content.");
                deleteConfirmModalPanel.setCookieName("modal-2");

                deleteConfirmModalPanel.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {
                    public boolean onCloseButtonClicked(AjaxRequestTarget target) {
                        return true;
                    }
                });

                deleteConfirmModalPanel.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
                    public void onClose(AjaxRequestTarget target) {
                        System.out.println("onClose"); // TODO REMOVE debug line
                    }
                });

                AjaxLink removeLink = new AjaxLink<String>("removeLink") {
                    public void onClick(AjaxRequestTarget ajaxRequestTarget) {
                        //ClassroomListPanel.this.modelDao.delete(classroom);
                        //ajaxRequestTarget.addComponent(ClassroomListPanel.this);
                        deleteConfirmModalPanel.show(ajaxRequestTarget);
                    }
                };
                removeLink.setVisible(ClassroomListPanel.this.editMode);
                item.add(removeLink);
            }
        });

    }

    // ----- public methods -----

    public boolean isEditMode() {
        return this.editMode;
    }

    public void toggleEditMode() {
        this.editMode = !this.editMode;
    }

    // ----- inner classes -----

    private class ClassRoomDataProvider implements IDataProvider<Classroom> {

        // ----- instance fields -----

        private ModelDao localModelDao;

        // ----- constructors -----

        private ClassRoomDataProvider(ModelDao modelDao) {
            this.localModelDao = modelDao;
        }

        // ----- IDataProvider -----

        public Iterator<Classroom> iterator(int first, int count) {
            List<Classroom> list = this.localModelDao.getClassroomList(first, count);
            return list.iterator();
        }

        public int size() {
            return this.localModelDao.getClassroomCount();
        }

        public IModel<Classroom> model(Classroom classroom) {
            return new Model<Classroom>(classroom);
        }

        // ----- IDetachable -----

        public void detach() {
            System.out.println(""); // TODO REMOVE debug line
        }
    }

}
